package cn.admin.remoting.impl;


import cn.admin.remoting.Connection;
import cn.admin.remoting.IOEventListener;
import cn.admin.remoting.Remoting;

import java.net.InetSocketAddress;
import java.util.List;

class LCL_onClose4Reconnect implements IOEventListener.ConnectionLifecycleListener {

    private final ReconnectManager reconnectMgr;

    LCL_onClose4Reconnect() {
        DefaultClientManager clientMgr = (DefaultClientManager) Remoting.clientMgr();
        this.reconnectMgr = clientMgr.reconnectMgr;
    }

    @Override
    public void connectionOpened(Connection connection) {

    }

    @Override
    public void connectionClosed(Connection connection) {
        if (connection.isClientSide()) {
            if (null == connection.getAttribute("initClose"));
            String clientKey = connection.getClient().getClientKey();
            Object[] temp = ClientKeyUtil.parseClientKey(clientKey);
            String appName = (String) temp[0];
            String targetUrl = (String) temp[1];
            InetSocketAddress localAddress = (InetSocketAddress) temp[2];
            List<IOEventListener> ioEventListeners = connection.getConnectionEvtListeners();
            this.reconnectMgr.addReconnectTask(appName,targetUrl,localAddress,ioEventListeners);
        }
    }
}
